package commonAdmin.code.generator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 *http://blog.csdn.net/hailangamy/article/details/7647018
 *@author Jack
 *2016年3月1日下午11:30:45
 */
public class CodeGenerator {
	/**  
	 * @param args  
	 */
	public static void main(String[] args) {
		//System.out.println(System.getProperty("user.dir")+"============");   
		Configuration cfg = new Configuration();
		try {
			cfg.setClassForTemplateLoading(CodeGenerator.class, "/commonAdmin/code/generator"); //指定模板所在的classpath目录   
			cfg.setSharedVariable("upperFC", new UpperFirstCharacter()); //添加一个"宏"共享变量用来将属性名首字母大写   
			Template t = cfg.getTemplate("javabean.html"); //指定模板   
			FileOutputStream fos = new FileOutputStream(new File("d:/Student.java")); //java文件的生成目录   

			//模拟数据源   
			Map data = new HashMap();
			data.put("package", "edu"); //包名   
			data.put("className", "Student");

			List pros = new ArrayList();
			Map pro_1 = new HashMap();
			pro_1.put("proType", String.class.getSimpleName());
			pro_1.put("proName", "name");
			pros.add(pro_1);

			Map pro_2 = new HashMap();
			pro_2.put("proType", String.class.getSimpleName());
			pro_2.put("proName", "sex");
			pros.add(pro_2);

			Map pro_3 = new HashMap();
			pro_3.put("proType", Integer.class.getSimpleName());
			pro_3.put("proName", "age");
			pros.add(pro_3);

			data.put("properties", pros);
			t.process(data, new OutputStreamWriter(fos, "utf-8")); //   
			fos.flush();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}
}
